package com.asman.worker.gallery

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.asman.base.lib.glide.GlideApp
import com.asman.worker.R
import com.asman.worker.multishow.ImageTitle
import com.asman.worker.multishow.MultiShowActivity
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.layout_square_image.view.*

/**
 * Created by Darshan on 4/18/2015.
 */

class ImageAdapter(private val data: List<Image>) :
    RecyclerView.Adapter<ImageAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.layout_square_image, parent, false)
        )
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.setData(data[position])
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

        fun setData(item: Image) {
            with(view) {
                itemView.setOnClickListener { v ->
                    val images = data.map {
                        ImageTitle(url = it.path ?: "", title = "")
                    }
                    MultiShowActivity.start((context as Activity), images, data.indexOf(item))
                }
                GlideApp.with(itemView.context)
                    .load(item.path)
                    .apply(
                        RequestOptions()
                            .centerCrop()
                            .placeholder(ColorDrawable(Color.parseColor("#f0f0f0")))
//                            .diskCacheStrategy(DiskCacheStrategy.NONE)
//                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                            .format(DecodeFormat.PREFER_RGB_565)
                    )
                    .thumbnail(0.2f)
                    .into(imageView)
            }
        }
    }
}
